﻿#region License

// Droid Manager - An Android Phone Tools Suite
// Copyright (C) 2011
// 
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// 
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
// 
// You should have received a copy of the GNU General Public License
// along with this program.  If not, see <http://www.gnu.org/licenses/>.

#endregion

using System.Collections.Generic;
using DroidManager.Log;
using DroidManager.Data;
using System.IO;

namespace DroidManager.Plugins
{
    /// <summary>
    /// Handles Plugins and Modules
    /// </summary>
    internal class PluginEngine
    {
        private Logger _log = Logger.Instance;

        private List<Plugin> _plugins = new List<Plugin>();

        private static PluginEngine _instance;

        internal static PluginEngine Instance
        {
            get
            {
                if (_instance == null)
                {
                    return _instance = new PluginEngine();
                }
                else
                {
                    return _instance;
                }
            }
        }

        internal List<Plugin> Plugins
        {
            get { return _plugins; }
        }

        internal PluginEngine()
        {
            _instance = this;
            LoadPlugins(ref _plugins);
        }


        //private void LoadPlugins(ref List<Plugin> pluginlist)
        //{
        //    foreach (string file in Directory.GetFiles(GlobalVars.PluginPath))
        //    {
        //        Plugin plugin = new Plugin(file, true); //Loads plugin and allows threading
        //        if (plugin.Load())
        //        {
        //            pluginlist.Add(plugin);
        //        }
        //    }
        //}

        private void LoadPlugins(ref List<Plugin> pluginlist)
        {
            foreach (string folder in Directory.GetDirectories(GlobalVars.PluginPath))
            {
                string foldername = folder.Replace(GlobalVars.PluginPath + @"\", "");
                if (File.Exists(folder + @"\" + foldername + ".dll"))
                {
                    Plugin plugin = new Plugin(folder + @"\" + foldername + ".dll", true);
                    if (plugin.Load())
                    {
                        pluginlist.Add(plugin);
                    }
                }
                else if (File.Exists(folder + @"\" + foldername + ".exe"))
                {
                    Plugin plugin = new Plugin(folder + @"\" + foldername + ".exe", true);
                    if (plugin.Load())
                    {
                        pluginlist.Add(plugin);
                    }
                }
                else
                {
                    _log.Warning("No plugins in folder \"" + folder + "\", Check assemblies name!");
                }
            }
        }

        internal Plugin GetPluginByName(string name)
        {
            foreach (Plugin plugin in Plugins)
            {
                if (plugin.Name == name)
                {
                    return plugin;
                }
            }
            return null;
        }
    }
}